Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

1.14 Pipes and Redirection

| — The Pipe Command

Format:

command1 | command2

Its architectural function is to direct the standard output (stdout) of the first command (command1) to serve as the standard input (stdin) for the second command (command2).

Example:

$ ps aux | grep nginx

> and >> — Output Redirection Commands

Examples:

$ ls > file.txt # Overwrites or creates file.txt with the stdout of ls

$ ls >> file.txt # Appends the stdout of the ls command to the end of file.txt

$ ps -ef | grep root | grep ssh | wc -l # Multi-stage pipeline
execution

Within a Linux / Unix Shell environment, every running process maps to exactly three default standard file descriptors:

Descriptor NameNumeric VectorArchitectural Meaning
stdin0Standard Input (file or hardware input device)
stdout1Standard Output (file or hardware display device)
stderr2Standard Error (file or hardware diagnostic logging device)

These three processing channels can be controlled via their symbolic descriptor descriptors or explicitly referenced by their numeric index values. Leveraging the redirection syntax (> or >>), standard I/O channels can be decoupled from default hardware and mapped to alternative devices or physical files. The implementation syntax is defined as follows:

command 2> error.txt # Redirects the stderr stream of a command into error.txt

Examples:

$ ls not_exist 2 > err.log

command > out.txt 2>&1

(Note: In the final example, the primary output stream is directed into out.txt. Simultaneously, the stderr stream (2>) is mapped to the exact same channel destination currently assigned to stdout (&1), which typically outputs directly to the screen terminal).

The & symbol acts as a dedicated file descriptor token, informing the Shell interpreter that the trailing argument integer is an active file descriptor rather than a literal, regular filename string. Without appending the & token (e.g., writing 2>1), the Shell would parse the instruction as a request to dump diagnostic error tracking outputs into a common text file literally named 1.

grep keyword file 2>/dev/null # Redirects stderr to the bit-bucket null device to suppress errors

Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com